#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;

class Number
{
public:
    Number(int data)
    : _data(data)
    {}

    void print() const
    {   cout << _data << " ";}

    bool isEven() const
    {
        return (_data % 2 == 0);
    }

    bool isPrime() const
    {
        if(_data == 1) 
            return false;
        else {
            for(int i = 2; i <= _data / 2; ++i) {
                if( _data % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }

private:
    int _data;
};

void display(Number & num)
{
    num.print();
}

int main()
{
    vector<Number> vec;
    for(int i = 1; i < 20; ++i) {
        vec.emplace_back(i);
    }

    /* for_each(vec.begin(), vec.end(),display); */
    //直接使用成员函数的写法，是做不到的,因为传递过去的是一个成员函数指针
    /* for_each(vec.begin(), vec.end(), &Number::print); */
    //针对于容器中的元素是自定义类类型时,比较方便
    for_each(vec.begin(), vec.end(), std::mem_fn(&Number::print));
    cout << endl;

    vec.erase(std::remove_if(vec.begin(), vec.end(), std::mem_fn(&Number::isEven)),
              vec.end());
    for_each(vec.begin(), vec.end(), std::mem_fn(&Number::print));
    cout << endl;

    vec.erase(std::remove_if(vec.begin(), vec.end(), std::mem_fn(&Number::isPrime)),
              vec.end());
    for_each(vec.begin(), vec.end(), std::mem_fn(&Number::print));
    cout << endl;

    return 0;
}
